package org.jeecg.modules.oms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.oms.service.IFinanceStatisticsService;
import org.jeecg.modules.oms.vo.FinanceStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 财务统计控制器
 * @Author: jeecg-boot
 * @Date: 2025-01-13
 * @Version: V1.0
 */
@Api(tags = "财务统计")
@RestController
@RequestMapping("/statistics/finance")
@Slf4j
public class FinanceStatisticsController {

    @Autowired
    private IFinanceStatisticsService financeStatisticsService;

    // ==================== 应收应付专用接口 ====================

    @ApiOperation(value = "获取应收应付汇总统计数据", notes = "获取应收应付汇总统计数据")
    @GetMapping(value = "/receivable-payable/summary")
    public Result<Map<String, Object>> getReceivablePayableSummary(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            Map<String, Object> result = financeStatisticsService.getReceivablePayableSummary(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应收应付汇总统计数据失败", e);
            return Result.error("获取应收应付汇总统计数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取应收账款树形结构数据", notes = "获取应收账款树形结构数据（客户-工程-订单三级）")
    @GetMapping(value = "/receivable/tree")
    public Result<List<Map<String, Object>>> getReceivableTreeData(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = financeStatisticsService.getReceivableTreeData(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应收账款树形结构数据失败", e);
            return Result.error("获取应收账款树形结构数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取应付账款树形结构数据", notes = "获取应付账款树形结构数据（供应商-采购单-采购明细三级）")
    @GetMapping(value = "/payable/tree")
    public Result<List<Map<String, Object>>> getPayableTreeData(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = financeStatisticsService.getPayableTreeData(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应付账款树形结构数据失败", e);
            return Result.error("获取应付账款树形结构数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取应收账款概览数据", notes = "获取应收账款概览数据")
    @GetMapping(value = "/receivable/overview")
    public Result<FinanceStatisticsVO.ReceivableOverviewVO> getReceivableOverview(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            FinanceStatisticsVO.ReceivableOverviewVO result = financeStatisticsService.getReceivableOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应收账款概览数据失败", e);
            return Result.error("获取应收账款概览数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取应付账款概览数据", notes = "获取应付账款概览数据")
    @GetMapping(value = "/payable/overview")
    public Result<FinanceStatisticsVO.PayableOverviewVO> getPayableOverview(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            FinanceStatisticsVO.PayableOverviewVO result = financeStatisticsService.getPayableOverview(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应付账款概览数据失败", e);
            return Result.error("获取应付账款概览数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取应收应付对比图表数据", notes = "获取应收应付对比图表数据")
    @GetMapping(value = "/receivable-payable/comparison")
    public Result<List<Map<String, Object>>> getReceivablePayableComparisonData(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "统计周期", example = "month") @RequestParam(value = "period", defaultValue = "month") String period,
            HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = financeStatisticsService.getReceivablePayableComparisonData(startDate, endDate, period);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取应收应付对比图表数据失败", e);
            return Result.error("获取应收应付对比图表数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取账期分析图表数据", notes = "获取账期分析图表数据")
    @GetMapping(value = "/aging/analysis")
    public Result<List<Map<String, Object>>> getAgingAnalysisData(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = financeStatisticsService.getAgingAnalysisData(startDate, endDate);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取账期分析图表数据失败", e);
            return Result.error("获取账期分析图表数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "获取资金流动性分析数据", notes = "获取资金流动性分析数据")
    @GetMapping(value = "/liquidity/analysis")
    public Result<List<FinanceStatisticsVO.LiquidityItem>> getLiquidityAnalysis(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "统计周期", example = "month") @RequestParam(value = "period", defaultValue = "month") String period,
            HttpServletRequest request) {
        try {
            List<FinanceStatisticsVO.LiquidityItem> result = financeStatisticsService.getLiquidityAnalysis(startDate, endDate, period);
            return Result.OK(result);
        } catch (Exception e) {
            log.error("获取资金流动性分析数据失败", e);
            return Result.error("获取资金流动性分析数据失败: " + e.getMessage());
        }
    }

    @ApiOperation(value = "导出应收应付明细数据", notes = "导出应收应付明细数据到Excel")
    @GetMapping(value = "/receivable-payable/export")
    public void exportReceivablePayableData(
            @ApiParam(value = "开始日期", example = "2024-01-01") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(value = "结束日期", example = "2024-12-31") @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request,
            HttpServletResponse response) {
        try {
            financeStatisticsService.exportReceivablePayableData(startDate, endDate, response);
        } catch (Exception e) {
            log.error("导出应收应付明细数据失败", e);
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
    }
}
